home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
simula
/
books
/
books.lha
/
kirkerud
/
quicksorttest.sim
< prev
next >
Wrap
Text File
|
1993-08-16
|
2KB
|
71 lines
begin
procedure Quicksort(table, low_bound, high_bound);
integer array table; integer low_bound, high_bound;
! Sorts the elements in table(low_bound : high_bound) in non-decreasing order;
if low_bound < high_bound then
begin integer some_value, last_below, last_equal, first_above, ind, x;
some_value := table(low_bound);
last_below := low_bound - 1;
last_equal := low_bound;
first_above := high_bound + 1;
ind := low_bound + 1;
while ind < first_above do
begin
x := table(ind);
if x < some_value then
begin
last_below := last_below + 1; last_equal := last_equal + 1;
table(ind) := table(last_below); table(last_below) := x;
ind := ind + 1;
end else
if x = some_value then
begin last_equal := last_equal + 1; ind := ind + 1 end
else begin
first_above := first_above - 1;
table(ind) := table(first_above); table(first_above) := x;
end;
end;
Quicksort(table, low_bound, last_below);
Quicksort(table, first_above, high_bound);
end of Quicksort;
procedure qt(a, low, hi); integer array a; integer low, hi;
begin integer ind, error_count; Boolean sort_error;
outtext("Before sorting: "); outimage;
for ind := low step 1 until hi do outint(a(ind), 5);
outimage;
Quicksort(a, low, hi);
outtext("After sorting: "); outimage;
for ind := low step 1 until hi do outint(a(ind), 5);
outimage;
for ind := low + 1 step 1 until hi do
if a(ind-1) > a(ind) then
begin
if not sort_error then
begin outtext("First sort-error at index "); outint(ind-1, 0) end;
sort_error := true;
error_count := error_count + 1;
end;
if not sort_error
then outtext("Sorted correctly!")
else begin outint(error_count, 0); outtext(" sort-errors!") end;
outimage; outimage;
end;
integer array a(1 : 50);
integer ind, seed;
for ind := 1 step 1 until 50 do a(ind) := ind;
qt(a, 1, 50);
for ind := 1 step 1 until 50 do a(ind) := -ind;
qt(a, 1, 50);
seed := 123579;
for ind := 1 step 1 until 50 do a(ind) := randint(1, 50, seed);
qt(a, 1, 50);
end